package com.gitee.cui.redis.jedis;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import redis.clients.jedis.Jedis;

/**
 * Redis中Set类型测试
 * @author cuiqingqiang
 * @version 1.0
 * @date 2021/11/7 20:05
 **/
@Slf4j
public class SetTest {

    private static final String KEY = "set1";

    @Test
    public void setTest() {
        Jedis jedis = new Jedis("localhost", 6379);
        log.info("jedis.ping(): " + jedis.ping());

        jedis.del(KEY);
        // 取得类型，none
        log.info("jedis.type(KEY): " + jedis.type(KEY));
        // 向集合增加元素
        jedis.sadd(KEY, "user1", "user2", "user3");
        // 取得类型，set
        log.info("jedis.type(KEY): " + jedis.type(KEY));
        // 遍历所有元素
        log.info("jedis.smembers(KEY): " + jedis.smembers(KEY));
        // 获取集合元素个数
        log.info("jedis.scard(KEY): " + jedis.scard(KEY));
        // 判断元素是否在集合中
        log.info("jedis.sismember(KEY, \"user1\"): " + jedis.sismember(KEY, "user1"));
        // 删除元素
        log.info("jedis.srem(KEY, \"user2\"): " + jedis.srem(KEY, "user2"));
        // 遍历所有元素
        log.info("jedis.smembers(KEY): " + jedis.smembers(KEY));

        jedis.close();
    }
}
